import java.io.*;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author huangxin
 * @date 2019/6/19-14:13
 * 稀疏数组
 */
public class SparseArray {
    public static void main(String[] args) throws IOException {
        test2();
    }

    private static void test2() {
        int[][] arr = new int[6][10];
        arr[1][3] = 1;
        arr[2][3] = 5;

        System.out.println("原始数组");
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] != 0) {
                    sum++;
                }
                System.out.printf("%d\t", arr[i][j]);
            }
            System.out.println();
        }

        //比有效数据的个数多1行,用于存储行列和个数
        int[][] sparseArr = new int[sum + 1][3];
        sparseArr[0][0] = arr.length; //行
        sparseArr[0][1] = arr[0].length; //列
        sparseArr[0][2] = sum; //列

        //下标
        int index = 1;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] != 0) {
                    //存储数据
                    sparseArr[i][0] = i;
                    sparseArr[i][1] = j;
                    sparseArr[i][2] = arr[i][j];
                    index++;
                }
            }
        }

        for (int i = 0; i < sparseArr.length; i++) {
            System.out.println(Arrays.toString(sparseArr[i]));
        }

        //恢复数组
        int[][] newArray = new int[sparseArr[0][0]][sparseArr[0][1]];
        for (int i = 1; i < sparseArr.length; i++) {
            for (int j = 0; j < sparseArr[i].length; j++) {
                int row = sparseArr[i][0];
                int column = sparseArr[i][1];
                int value = sparseArr[i][2];
                newArray[row][column] = value;
            }
        }

        for (int i = 0; i < newArray.length; i++) {
            System.out.println(Arrays.toString(newArray[i]));
        }
    }

    public static void test1() {
        int[][] arr = new int[11][4];
        arr[1][3] = 1;
        arr[2][3] = 5;
        //数组里的有效数据
        System.out.println("原始数组");
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] != 0) {
                    sum++;
                }
                System.out.printf("%d\t", arr[i][j]);
            }
            System.out.println();
        }

        System.out.printf("\n有效数据为%d个\n", sum);

        //稀疏数组,第一行记录 数组的长度和  有效值的个数
        int[][] sparseArr = new int[sum + 1][3];
        sparseArr[0][0] = arr.length;
        sparseArr[0][1] = arr[0].length;
        sparseArr[0][2] = sum;
        //下标
        int index = 0;

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if (arr[i][j] != 0) {
                    index++;
                    sparseArr[index][0] = i;
                    sparseArr[index][1] = j;
                    sparseArr[index][2] = arr[i][j];
                }
            }
        }

        System.out.println();


        System.out.println("稀疏过的数组");
        for (int i = 0; i < sparseArr.length; i++) {
            System.out.printf("%d\t%d\t%d\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
        }


//        //io存储读取
//        String path = "d:/map.data";
//        FileWriter writer = new FileWriter(path);
//        for (int[] ints : sparseArr) {
//            String join = String.join(",", Arrays.stream(ints).boxed()
//                    .map(Objects::toString).collect(Collectors.toList()).toArray(new String[]{})) + "\n";
//            writer.append(join);
//        }
//
//        writer.flush();
//        writer.close();
//
//
//        Reader reader = new FileReader(path);
//        char[] chars = new char[10];
//        int len = 0;
//        String result = "";
//        while ((len = reader.read(chars)) > 0) {
//            result += new String(chars, 0, len);
//        }
//
//
//        String[] split = result.split("\n");
//        sparseArr = new int [split.length][3];
//        for (int i = 0; i < split.length; i++) {
//            Object[] array = Arrays.stream(split[i].split(",")).map(Integer::parseInt).collect(Collectors.toList()).toArray();
//            sparseArr[i][0] = (int) array[0];
//            sparseArr[i][1] = (int) array[1];
//            sparseArr[i][2] = (int) array[2];
//        }


        //恢复
        int[][] backArr = new int[sparseArr[0][0]][sparseArr[0][1]];

        for (int i = 1; i < sparseArr.length; i++) {
            int row = sparseArr[i][0];
            int column = sparseArr[i][1];
            backArr[row][column] = sparseArr[i][2];
        }


        System.out.println("恢复的数组");
        for (int i = 0; i < backArr.length; i++) {
            for (int j = 0; j < backArr[i].length; j++) {
                System.out.printf("%d\t", arr[i][j]);
            }
            System.out.println();
        }
    }
}
